//
//  DotLineView.m
//  DrawDotLine-OC
//
//  Created by 岚海网络 on 16/2/18.
//  Copyright © 2016年 岚海网络. All rights reserved.
//

#import "DotLineView.h"

@implementation DotLineView

// 一定要重写这个方法
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        // 初始化 ...
        
        [self drwaDotLine];
    }
    
    return self;
}


- (void)drwaDotLine {

    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:self.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    
    // 设置虚线的颜色
    [shapeLayer setStrokeColor:[[UIColor lightGrayColor] CGColor]];
    // 设置虚线的宽度
    [shapeLayer setLineWidth:1.0];
    [shapeLayer setLineJoin:kCALineCapRound];
    
    //3=线的宽度  2=线的间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:3], [NSNumber numberWithInt:2], nil]];
    
    CGMutablePathRef path = CGPathCreateMutable();
    // 设置虚线的起始点
    CGPathMoveToPoint(path, NULL, self.frame.origin.x, self.frame.origin.y);
    // 设置虚线的终点
    CGPathAddLineToPoint(path, NULL, self.frame.origin.x + self.frame.size.width, self.frame.origin.y);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    [self.layer addSublayer:shapeLayer];
}

@end
